﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using moc.Common;
using moc.IBLL;
using moc.IDAL;
using moc.Models;

namespace moc.BLL
{
    public partial class AdminInfoBll :BaseBll,IAdminInfoBll
    {
        
        private readonly IAdminInfoDal _dal;

        public AdminInfoBll(IUnitOfWork unitOfWork,IAdminInfoDal dal) : base(unitOfWork)
        {
            _dal = dal;
        }


        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public bool AddAdmin(AdminInfo admin)
        {
            _dal.Add(admin);
            return (_unitOfWork.SaveChanges() > 0);
        }

        /// <summary>
        /// 删除管理员
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool DeleteAdmin(List<AdminInfo> list)
        {
            _dal.Delete(list);
            return (_unitOfWork.SaveChanges() > 0);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool EditPwd(string password)
        {
            AdminInfo admin = new AdminInfo()
            {
                Password=password
            };
            _dal.Update(admin);
            return (_unitOfWork.SaveChanges() > 0);
        }

        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public bool EditRole(string roleId)
        {
            AdminInfo admin = new AdminInfo()
            {
                Role_id=int.Parse(roleId)
            };
            _dal.Update(admin);
            return (_unitOfWork.SaveChanges() > 0);
        }

        /// <summary>
        /// 获取管理员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable<AdminInfo> GetAdmin(int id)
        {
            return _dal.LoadEntities(u => u.Admin_id == id);
        }

        /// <summary>
        /// 管理员信息分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public IQueryable<AdminInfo> LoadPage(PageParam page, out int total)
        {
            IQueryable<AdminInfo> pageData;
            switch (page.Sort)
            {
                case nameof(AdminInfo.Admin_name):
                    pageData = _dal.LoadPage(page.PageSize, page.PageIndex, out total, u => u.Admin_name == page.SearchText, u => u.Admin_name, page.Order);
                    break;
                default:
                    pageData = _dal.LoadPage(page.PageSize, page.PageIndex, out total, u => u.Admin_name == page.SearchText, u => u.Admin_id, page.Order);
                    break;
            }
            return pageData;

        }
    }
}
